package com.wcarmon.utils {

 	import mx.utils.ObjectUtil;
 	import mx.utils.StringUtil;
 	import mx.formatters.DateFormatter;
 	//import flash.utils.*;
 	import mx.logging.*;
 	
	public class Utils {

		//include "../includes/loggerSnippet.as";
		public static var sLog : ILogger = Log.getLogger("com.wcarmon.utils.Utils");
		

		public static const millisecondsPerMinute : int = 1000 * 60;
		public static const millisecondsPerHour : int = 1000 * 60 * 60;
		public static const millisecondsPerDay : int = 1000 * 60 * 60 * 24;
		
		public static function isBefore( d1 : Date, d2 : Date ) : Boolean{
			var df : DateFormatter = new DateFormatter();
			df.formatString = "YYYY-MM-DD";
			var s1 : String = df.format( d1 ); 
			var s2 : String = df.format( d2 );
			return s1 < s2;
		}	
		
		//Tested 12/12
		public static function nDaysAgo( days : uint ) : Date{
			var now : Date = new Date(); 
			return new Date( now.getTime() - (days * millisecondsPerDay) );
		}	
		
		//Tested 12/12
		public static function nDaysAgoStr( days : uint, frmt : String = 'YYYY-MM-DD' ) : String {
			var tmp : Date = nDaysAgo( days );
			var df : DateFormatter = new DateFormatter();
			df.formatString = frmt;
			return df.format( tmp );
		}
		
		//test this
		public static function dateDiffInDays( d1 : Date, d2 : Date ) : int {
			sLog.debug("calculating difference between dates: " + d1 + " and " + d2);
			return Math.floor( (d1.getTime() - d2.getTime()) / millisecondsPerDay); 		
		}
		

		//Givon: fix this to not use days/seconds
		public static function nMonthsAgo( days : uint ) : Date{
			sLog.warn("GIVON: implement Utils.nMonthsAgo!");
			var now : Date = new Date(); 
			//return new Date( now.getTime() - (days * millisecondsPerDay) );
			return null;
		}	
		
		public static function isToday( dateStr : String ) : Boolean {
			var df : DateFormatter = new DateFormatter();
			df.formatString = "YYYY-MM-DD";
			return (df.format( new Date() ) == dateStr.substr(0,10) ); 
		}
		
		//Givon: test this
		public static function isInThePast( dateStr : String ) :Boolean {
			if( isEmptyDate(dateStr) ){ return false; }
			var df : DateFormatter = new DateFormatter();
			df.formatString = "YYYY-MM-DD";
			return ( df.format(new Date()) > dateStr.substr(0,10) );
		}
		
		public static function isEmptyDate( dateStr : String ) : Boolean{
			sLog.debug("checking if '" +dateStr+ "' is an empty date..." );
			var s : String = StringUtil.trim( dateStr );
			if( s == '' ){ 
				sLog.debug("yep it's empty"); 
				return true; 
			}
			if( s.substr(0,10) == '0000-00-00' ){ 
				sLog.debug("yep it's empty"); 
				return true; 
			}
			if( s == '0000-00-00 00:00:00' ){ 
				sLog.debug("yep it's empty"); 
				return true; 
			}			
			sLog.debug("not empty date");
			return false;
		}
		
		public static function shrinkString( s : String, limit : uint ) : String{
			if( s.length <= limit ){
				return s;
			}		
			return s.substr(0, limit - 3) + '...';
		}
		
		public static function empty( s:String ) : Boolean{
			return (0 == StringUtil.trim(s).length);
		}
	}
}